﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.Model;
using Gift.DAL;
using Gift.Security;

namespace WebSite.m
{
    public partial class b : System.Web.UI.Page
    {
       public string MobileOrderCode = string.Empty;
       public string MobileUserName = string.Empty;
       public string Mobile = string.Empty;
       public string MobileAddress = string.Empty;
       public string MobileProductName = string.Empty;
       public string MobileRemark = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            MobileOrderCode = Session["mobileOrderCode"].ToString();
            MobileUserName = Session["mobileUserName"].ToString();
            Mobile = Session["mobilePhone"].ToString();
            MobileAddress = Session["mobileAddress"].ToString();
            MobileProductName = Session["mobileProductName"].ToString();
            MobileRemark = Session["mobileRemark"].ToString();

            if(Mobile.Length == 11)
            {
                this.txtMobile.Text = Mobile;
            }

        }



        sms.WebService theSMS = new sms.WebService();
        string sn = System.Configuration.ConfigurationManager.AppSettings["GMSSN"];// "SDK-BBX-010-13406";//序列号
        string password = System.Configuration.ConfigurationManager.AppSettings["GMSPas"];//"413717";//密码
        string mobile = "";//手机
        string cont = "";//内容
        string subcode = "";//扩展码
        string stime = "";//定时时间

        string sContentFormat = "上海叶盟实业提醒：您在上海叶盟实业网站上兑换了一份{0}的礼品，定单号：{1}。客服：400-033-6966";
        Card theCard = new Card();


        protected void btnSend_Click(object sender, EventArgs e)
        {
           
            //获取参数
            getValues();
            //MD5加密密码
            string pwd = getMD5(sn + password);

            long startTime, endTime;
            startTime = DateTime.Now.Ticks;//记录当前时刻

            //发送短信
            //定时时间，扩展码和rrid可以为空，当rrid(唯一标识串)为空时，返回系统指定rrid
            string result = theSMS.mt(sn, pwd, mobile, cont, subcode, stime, "");

            endTime = DateTime.Now.Ticks;
            TimeSpan sendSpan = new TimeSpan(endTime - startTime);
            StringBuilder sbOutput = new System.Text.StringBuilder();
            if (result.StartsWith("-"))
            {
                   Response.Write(result);
                sbOutput.Append(result + "\r");
            }
            else
            {
            
                sbOutput.Append("发送成功" + "\r");
                sbOutput.Append("发送用时" + sendSpan.ToString() + "s\r");
            }
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        private void getValues()
        {
            mobile = this.txtMobile.Text;
            // subcode = tex_subcode.Text;
            // sn = tex_sn.Text.Trim();
            //password = tex_pwd.Text.Trim();
            //if (sch_time.Enabled)
            //{
            //    //取得定时时间
            //    stime = sch_time.Text;
            //}

          
            cont = string.Format(sContentFormat
                                 , MobileProductName
                                  ,MobileOrderCode
                                  );

        }

        /// <summary>
        /// 获取md5码
        /// </summary>
        /// <param name="source">待转换字符串</param>
        /// <returns>md5加密后的字符串</returns>
        public string getMD5(string source)
        {
            string result = "";
            try
            {
                MD5 getmd5 = new MD5CryptoServiceProvider();
                byte[] targetStr = getmd5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(source));
                result = BitConverter.ToString(targetStr).Replace("-", "");
                return result;
            }
            catch (Exception)
            {
                return "0";
            }

        }


    }
}